package com.workstudy.wstudy.controller;

import java.util.List;

import com.workstudy.common.core.domain.entity.SysUser;
import com.workstudy.wstudy.domain.WstCreditUser;
import com.workstudy.wstudy.domain.WstStudent;
import com.workstudy.wstudy.service.IWstStudentService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.workstudy.common.annotation.Log;
import com.workstudy.common.enums.BusinessType;
import com.workstudy.wstudy.domain.WstCredit;
import com.workstudy.wstudy.service.IWstCreditService;
import com.workstudy.common.core.controller.BaseController;
import com.workstudy.common.core.domain.AjaxResult;
import com.workstudy.common.utils.poi.ExcelUtil;
import com.workstudy.common.core.page.TableDataInfo;

/**
 * 信用管理Controller
 * 
 * @author sun
 * @date 2021-04-04
 */
@Controller
@RequestMapping("/wstudy/credit")
public class WstCreditController extends BaseController
{
    private String prefix = "wstudy/credit";

    @Autowired
    private IWstCreditService wstCreditService;

    @Autowired
    private IWstStudentService studentService;

    @RequiresPermissions("wstudy:credit:view")
    @GetMapping()
    public String credit()
    {
        return prefix + "/credit";
    }


    @GetMapping("/student")
    public String creditStudent(ModelMap map)
    {
        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        WstStudent student =studentService.selectWstStudentById(user.getUserId());
        map.put("student",student);
        return prefix + "/credit_student";
    }

    /**
     * 查询信用管理列表
     */
    @RequiresPermissions("wstudy:credit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WstCredit wstCredit)
    {
        startPage();
        List<WstCredit> list = wstCreditService.selectWstCreditList(wstCredit);
        return getDataTable(list);
    }

    /**
     * 查询学生信用管理列表
     */
    @PostMapping("/list/student")
    @ResponseBody
    public TableDataInfo listStudent(WstCreditUser creditUser)
    {
        startPage();
        List<WstCreditUser> list = wstCreditService.selectWstCreditUserList(creditUser);
        return getDataTable(list);
    }

    /**
     * 查询学生信用管理列表
     */
    @PostMapping("/list/student/person")
    @ResponseBody
    public TableDataInfo listStudentPerson(WstCreditUser creditUser)
    {
        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        creditUser.setUserId(user.getUserId());
        startPage();
        List<WstCreditUser> list = wstCreditService.selectWstCreditUserList(creditUser);
        return getDataTable(list);
    }

    @PostMapping("/listScore")
    @ResponseBody
    public AjaxResult listScore(WstCredit wstCredit)
    {
        startPage();
        List<WstCredit> list = wstCreditService.selectWstCreditList(wstCredit);
        return AjaxResult.success("查询列表",list);
    }

    /**
     * 导出信用管理列表
     */
    @RequiresPermissions("wstudy:credit:export")
    @Log(title = "信用管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WstCredit wstCredit)
    {
        List<WstCredit> list = wstCreditService.selectWstCreditList(wstCredit);
        ExcelUtil<WstCredit> util = new ExcelUtil<WstCredit>(WstCredit.class);
        return util.exportExcel(list, "credit");
    }

    /**
     * 新增信用管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    @GetMapping("/evaluate_add/{studentId}")
    public String evaluatePage(@PathVariable("studentId")Long studentId,ModelMap map)
    {
        map.put("userId",studentId);
        return prefix + "/evaluate_add";
    }

    /**
     * 新增用户评价
     */
    @PostMapping("/evaluate")
    @ResponseBody
    public AjaxResult evaluate(WstCreditUser creditUser)
    {
        return toAjax(wstCreditService.insertWstCreditUser(creditUser));
    }

    /**
     * 新增保存信用管理
     */
    @RequiresPermissions("wstudy:credit:add")
    @Log(title = "信用管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WstCredit wstCredit)
    {
        return toAjax(wstCreditService.insertWstCredit(wstCredit));
    }

    /**
     * 修改信用管理
     */
    @GetMapping("/edit/{creditId}")
    public String edit(@PathVariable("creditId") Long creditId, ModelMap mmap)
    {
        WstCredit wstCredit = wstCreditService.selectWstCreditById(creditId);
        mmap.put("wstCredit", wstCredit);
        return prefix + "/edit";
    }

    /**
     * 修改保存信用管理
     */
    @RequiresPermissions("wstudy:credit:edit")
    @Log(title = "信用管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WstCredit wstCredit)
    {
        return toAjax(wstCreditService.updateWstCredit(wstCredit));
    }

    /**
     * 删除信用管理
     */
    @RequiresPermissions("wstudy:credit:remove")
    @Log(title = "信用管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wstCreditService.deleteWstCreditByIds(ids));
    }
}
